import java.util.ArrayList;
import java.util.List;

/**
 * Map实现性能比较测试
 *
 * @author Kevin
 * @date 2018-12-16
 */
public class MapCompareTest {

    public static void main(String[] args) {
        String fileName = "pride-and-prejudice.txt";

        Map<String, Integer> bstMap = new BSTMap<>();
        double time1 = testMap(bstMap, fileName);
        System.out.println("BST Map: " + time1 + " s");

        Map<String, Integer> linkedListMap = new LinkedListMap<>();
        double time2 = testMap(linkedListMap, fileName);
        System.out.println("LinkedList Map: " + time2 + " s");
    }

    /**
     * 测试map
     */
    private static double testMap(Map<String, Integer> map, String fileName) {
        long startTime = System.nanoTime();

        System.out.println(fileName);
        List<String> words = new ArrayList<>();
        if (FileOperation.readFile(fileName, words)) {
            System.out.println("Total words: " + words.size());

            for (String word : words) {
                if (map.contains(word)) {
                    map.set(word, map.get(word) + 1);
                } else {
                    map.add(word, 1);
                }
            }

            System.out.println("Total different words: " + map.getSize());
            System.out.println("Frequency of PRIDE: " + map.get("pride"));
            System.out.println("Frequency of PREJUDICE: " + map.get("prejudice"));
        }

        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

}
